import { IHttpResponse, IObject } from "./interface";
import { Method } from "axios";
import http from "./http";
import {getToken} from "./cache";

/**
 * 常用CRUD
 */
export default {
    /**
     * 删除
     * @param path
     * @param params
     * @returns
     */
    delete(path: string, params: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            data: params,
            headers:{"tokenId":getToken()},
            method: "DELETE"
        });
    },
    get(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return new Promise((resolve, reject) => {
            http({
                url: path,
                params,
                headers:{"tokenId":getToken()},
                method: "GET"
            })
                .then(resolve)
                .catch((error) => {
                    if (error !== "-999") {
                        reject(error);
                    }
                });
        });
    },
    put(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            data: params,
            headers: {
                "Content-Type": "application/json;charset=UTF-8",
                "tokenId":getToken(),
                ...headers
            },
            method: "PUT"
        });
    },
    /**
     * 通用post方法
     * @param path
     * @param body
     * @returns
     */
    post(path: string, body?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            method: "post",
            headers: {
                "Content-Type": "application/json;charset=UTF-8",
                "tokenId":getToken(),
                ...headers
            },
            data: body
        });
    },

    /**
     * 下载文件
     * @param {*} url
     * @param {*} params
     * @param {*} filename
     * @param {*} method
     */
    download(
        path: string,
        params?: IObject,
        filename?: string,
        method: Method = "get"
    ): Promise<IHttpResponse> {
        const opt: any = { url: path, params, method, responseType: "blob" };
        if (method.toLowerCase() === "post") {
            opt.data = params;
            delete opt.params;
        }

        return http(opt).then((res): any => {
            const a = document.createElement("a");
            const evt = document.createEvent("MouseEvents");
            evt.initEvent("click", false, false);
            a.download = filename || new Date().getTime().toString();
            a.href = URL.createObjectURL(res);
            a.dispatchEvent(evt);
        });
    },
    /**
     * 下载文件
     * @param {*} url
     * @param {*} params
     * @param {*} filename
     * @param {*} method
     */
    downloadOss(
        path: string,
        params?: IObject,
        filename?: string,
        method: Method = "get"
    ): Promise<IHttpResponse> {


        if(filename==null){
            const lastSlashIndex = path.lastIndexOf('/');
            filename==path.substring(lastSlashIndex + 1);
        }
        path="fii/front/user/download?url="+path;
        return this.download(path,{},filename);
    },dictList(dictCode:string,target:[]){
        this.get("").then((e:any)=>{
            target=e.data;
        });

    },
    areaList(target:any,callback?:Function){
        this.get("/front/fast/sys/areaDict/list?pid="+target.code).then((e:any)=>{
            target.item=e.data;
            if(callback){
                callback(e.data);
            }
           
        });

    }

};
